/* this file contains the ChatFragment activity, 
 * with the buttons and the history of message.
 * The layout is in the file chat_details.xml. 
 * The buttons are to be implemented in this file. */
package dk.aau.walkietalkieui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import dk.aau.walkietalkieui.R;
import dk.aau.walkietalkieui.adapter.MenuListAdapter;

public class ChatFragment extends Fragment {
	private static String KEY_SELECTED_INDEX = "selIndex";
	public static final String TAG = "ChatFragment";

	// To have the name corresponding to the current conversation
	private final MenuListAdapter adapter = new MenuListAdapter(getActivity());

	public static ChatFragment newInstance(int selectedIndex) {
		ChatFragment chatFg = new ChatFragment();
		Bundle args = new Bundle();

		args.putInt(KEY_SELECTED_INDEX, selectedIndex);
		chatFg.setArguments(args);
		return chatFg;

	}

	private int selectedIndex;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null) {
			selectedIndex = savedInstanceState.getInt(KEY_SELECTED_INDEX, -1);
		} else {
			Bundle args = getArguments();
			if (args != null) {
				selectedIndex = args.getInt(KEY_SELECTED_INDEX, -1);

			} else {
				selectedIndex = -1;
			}

		}
		// Displays the name of the contact in the conversation chat
		getActivity().setTitle(adapter.getItem(selectedIndex));
		getActivity().setTitleColor(getResources().getColor(R.color.white));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.chat_details, null);

		Button btnSend = (Button) root.findViewById(R.id.btnSend);
		btnSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Send Button

				// getActivity().supportInvalidateOptionsMenu();
				Toast.makeText(getActivity(), "Message Sent", Toast.LENGTH_LONG)
						.show();
			}
		});

		Button btnRecord = (Button) root.findViewById(R.id.btnRecord);
		btnRecord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Record Button
				Toast.makeText(getActivity(), "Record", Toast.LENGTH_LONG)
						.show();
			}
		});

		return root;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(KEY_SELECTED_INDEX, selectedIndex);
	}
}
